package com.xiaomei.manager.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xiaomei.manager.model.Label;
import com.xiaomei.manager.service.ILabelService;
import com.xiaomei.manager.web.dto.req.LabelDto;
import com.xiaomei.manager.web.page.Page;


@Controller
@RequestMapping("/label")
public class LabelController {
	@Autowired
	private ILabelService iLabelService;
	
	 @RequiresPermissions("label:view")
	 @RequestMapping()
	 public String list(Model model, LabelDto dto, Page<Label> page) {	    	
		  iLabelService.getByParamMap(page, dto);
	    	model.addAttribute("dto", dto);
	        model.addAttribute("page", page);     
	        return "label/list";
	    }
	 /**
	     * 添加
	     * @param model
	     * @return
	     */
		 @RequiresPermissions("label:create")
		    @RequestMapping( value = "/create",method = RequestMethod.GET)
		    public String showCreateForm(Model model) {	        
		        model.addAttribute("exp", new Label());
		        model.addAttribute("op", "新增");
		        return "label/edit";
		    }
		 @RequiresPermissions("label:create")
		    @RequestMapping(value = "/create",method = RequestMethod.POST)
		    public String create(Label exp , RedirectAttributes redirectAttributes) {     	
			 	iLabelService.createLabel(exp);
		        redirectAttributes.addFlashAttribute("msg", "新增成功");
		        return "redirect:/label";
		    }
		 /**
		     * 更新label图内容
		     * @param id
		     * @param model
		     * @return
		     */
		    @RequiresPermissions("label:update")
		    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
		    public String showUpdateForm(@PathVariable("id") Integer id, Model model) {
		        model.addAttribute("exp", iLabelService.findOne(id));
		        model.addAttribute("op", "修改");
		        return "label/edit";
		    }

		    @RequiresPermissions("label:update")
		    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
		    public String update(Label exp,  @PathVariable("id") Integer id, RedirectAttributes redirectAttributes, HttpServletRequest request){	    		    
		        iLabelService.updateLabel(exp);
		        redirectAttributes.addFlashAttribute("msg", "修改成功");
		        return "redirect:/label";
		    }
		    /**
		     * 删除n
		     * @param id
		     * @param model
		     * @return
		     */
		    @RequiresPermissions("label:delete")
		    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
		    public String showDeleteForm(@PathVariable("id") Integer id, Model model) {	       
		    	model.addAttribute("exp", iLabelService.findOne(id));
		        model.addAttribute("op", "删除");
		        return "label/edit";
		    }
		    
		    
		    
		    @RequiresPermissions("label:delete")
		    @RequestMapping(value="/{id}/delete" ,method=RequestMethod.POST)
		    public String delete(@PathVariable("id") Integer id,RedirectAttributes redirectAttributes ){
		    	 // label exp = ilabelService.findOne(id);
		    	iLabelService.deleteLabel(id);
			        redirectAttributes.addFlashAttribute("msg", "删除成功");
			        return "redirect:/label";
		    }
		

}
